<?php
import("@.Action.CommonAction");
class TableEditAction extends Common {
    public function index(){

		if($_SESSION['UserName'] != ""){
			$User = D("Users");
			$UserName = $_SESSION['UserName'];
			$UserInfo = $User->where("UserName ='$UserName'")->find();

			if($UserInfo['Role'] == "2")
			{
				$this->redirect("/ImportSheet/ViewParentsProduct");
			}
		}
		$TableName = $_GET['TableName'];
		$KeyID = $_GET['KeyID'];
		
		if($TableName == ""){
			$TableName = "ClassDate";
		}
		
		$Model = new Model(); // 实例化一个model对象 没有对应任何数据表

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   `Tables`.`Key` ");
		$sqlB->Append(" , `Tables`.Alias ");
		$sqlB->Append(" , `Tables`.`Table` ");
		$sqlB->Append("FROM `Tables` ");
		$sqlB->Append("WHERE `Tables`.`Table` = '" .$TableName. "' ");
		$sqlB->Append("Limit 1");
		$sqlB->Append(";");

		$TableInfo = $Model->query($sqlB->ToString());

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   TableFields.Field ");
		$sqlB->Append(" , TableFields.Alias ");
		$sqlB->Append("FROM Tables ");
		$sqlB->Append("INNER JOIN TableFields ON `Tables`.Table = TableFields.Table ");
		$sqlB->Append("   AND `Tables`.`Table` = '" .$TableName. "' ");
		$sqlB->Append("   AND `TableFields`.`Field` <> '" .$TableInfo[0]['Key']. "' ");
		$sqlB->Append("   AND `TableFields`.`Field` <> 'TimeUpdated' ");
		$sqlB->Append("   AND `TableFields`.`Field` <> 'UpdatedBy' ");
		$sqlB->Append("   AND `TableFields`.`Field` <> 'TimeCreated' ");
		$sqlB->Append("   AND `TableFields`.`Field` <> 'CreatedBy' ");
		$sqlB->Append("ORDER BY TableFields.`Sequence` ");
		$sqlB->Append(";");

		$TableFieldsInfo = $Model->query($sqlB->ToString());

		$TableList = null;

		if($KeyID != ""){
			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   * ");
			$sqlB->Append("FROM  `" .$TableName. "` AS TableName ");
			$sqlB->Append("WHERE `TableName`.`" . $TableInfo[0]['Key'] . "` = '" . $KeyID . "' ");
			$sqlB->Append(";");

			$TableList = $Model->query($sqlB->ToString());
		}

		$this->assign('TableInfo',$TableInfo);
		$this->assign('TableFieldsInfo',$TableFieldsInfo);
		$this->assign('TableList',$TableList);
		$this->display("TableEdit");
    }

	 public function Edit(){

		$List = $_GET['List'];
		$TableName = $_POST['TableName'];
		$KeyID = $_POST['KeyID'];

		unset($_POST['TableName']);
		unset($_POST['KeyID']);

		$Model = new Model(); // 实例化一个model对象 没有对应任何数据表

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   `Tables`.`Key` ");
		$sqlB->Append(" , `Tables`.Alias ");
		$sqlB->Append(" , `Tables`.`Table` ");
		$sqlB->Append("FROM `Tables` ");
		$sqlB->Append("WHERE `Tables`.`Table` = '" .$TableName. "' ");
		$sqlB->Append("Limit 1");
		$sqlB->Append(";");

		$TableInfo = $Model->query($sqlB->ToString());

		//Update
		if($KeyID != ""){
			if(is_numeric($KeyID) || $TableName == "TableFields"){

				
				$sqlB = new StringBuilder();
				$sqlB->Append("UPDATE ");
				$sqlB->Append("   `" . $TableName . "` AS TableName ");
				$sqlB->Append("SET  ");
				
				$Index = 0;

				foreach ($_POST as $Key => $Value)
				{
					if($Index > 0){
						$sqlB->Append(" , ");
					}

					$Index += 1;
					$sqlB->Append("TableName.`".$Key. "` = '" . $Value. "' ");
				}

				$sqlB->Append(" , TableName.`TimeUpdated` = NOW() ");
				$sqlB->Append(" , TableName.`UpdatedBy` = '". $_SESSION['UserName'] . "' ");
				$sqlB->Append("WHERE TableName.`" . $TableInfo[0]['Key'] . "` = '" . $KeyID . "' ");
				$sqlB->Append(";");
				
				$result = $Model->execute($sqlB->ToString());

				if($result === FALSE){
					$this->error('修改失败');
				} else 
				{
					if($List == "1"){
						$this->redirect("/TableView/Index/TableName/" . $TableName . "");
					} else
					{
						$this->redirect("/TableView/Index/TableName/" . $TableName . "/KeyID/" . $KeyID . "");
					}
				}
			}
		}
		else{
		//Insert
			$sqlB = new StringBuilder();
			$sqlB->Append("INSERT INTO ");
			$sqlB->Append("    " . $TableName . " ( ");
			$Index = 0;

			foreach ($_POST as $Key => $Value)
			{
				if($Index > 0){
					$sqlB->Append(", ");
				}

				$Index += 1;
				$sqlB->Append( $Key);
			}
			$sqlB->Append(" , TimeUpdated ");
			$sqlB->Append(" , UpdatedBy ");
			$sqlB->Append(" , TimeCreated ");
			$sqlB->Append(" , CreatedBy ");
			$sqlB->Append("   ) ");
			$sqlB->Append("SELECT ");
			
			$Index = 0;

			foreach ($_POST as $Key => $Value)
			{
				if($Index > 0){
					$sqlB->Append(", ");
				}

				$Index += 1;
				$sqlB->Append("'" . $Value. "' AS `".$Key. "` ");
			}
			$sqlB->Append(" , NOW() AS TimeUpdated ");
			$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS UpdatedBy ");
			$sqlB->Append(" , NOW() AS TimeCreated ");
			$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS CreatedBy ");
			$sqlB->Append(";");

			$result = $Model->execute($sqlB->ToString());

			if($result === FALSE){
				$this->error('添加失败');
			} else 
			{
				if($List == "1"){
					$this->redirect("/TableView/Index/TableName/" . $TableName . "");
				} else
				{
					$this->redirect("/TableView/Index/TableName/" . $TableName . "/KeyID/" . $KeyID . "");
				}
			}
		}
	 }

	 public function Deleted(){
		$TableName = strtolower($_GET['TableName']);
		$KeyID = $_GET['KeyID'];
		$Model = new Model($TableName);
		$result = $Model->delete($KeyID);
		if (FALSE !== $result){
			$this->redirect("/TableView/Index/TableName/" . $TableName . "");
		}else{
			$this->error('删除失败！');
		}
	 }
}